#!/usr/bin/env python
#coding:utf-8
import wx
import urllib2

class UrlInfoFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1,
                          'UrlInfo Frame', size=(480, 400))
        panel = wx.Panel(self, -1)
        #topLbl = wx.StaticText(panel, -1, 'URL信息查看器')
        #topLbl.SetFont(wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD))
        #wx.StaticLine(panel)
        urlText = wx.StaticText(panel, -1, 'url Enter:')
        self.urlInput = wx.TextCtrl(panel, -1, '', size=(320,-1))
        okButton = wx.Button(panel, wx.ID_OK, 'OK')
        t = wx.StaticText(panel, -1, 'Info:')
        self.infoText = wx.TextCtrl(panel, -1, size=(320,300), style=wx.TE_MULTILINE)
        cleanButton = wx.Button(panel, wx.ID_CANCEL, 'Clean')
        exitButton = wx.Button(panel, -1, '退出', (398,80))
        #urlInput.SetInsertionPoint(0)
        sizer = wx.FlexGridSizer(cols=3, hgap=10, vgap=10)
        sizer.AddMany([urlText, self.urlInput, okButton, t, self.infoText, cleanButton])
        panel.SetSizer(sizer)

        self.Bind(wx.EVT_BUTTON, self.OnButton, okButton)
        self.Bind(wx.EVT_BUTTON, self.OnClean, cleanButton)
        self.Bind(wx.EVT_BUTTON, self.OnExit, exitButton)
        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)

    def OnButton(self, event):
        url = self.urlInput.GetValue()
        info = self.geturlinfo(url)
        self.infoText.AppendText(info)

    def OnClean(self, event):
        self.infoText.Clear()

    def geturlinfo(self, url):
        sock = urllib2.urlopen(url)
        info = sock.info()
        return str(info) + '\n' + '-'*20

    def OnExit(self, event):
        self.Close(True)

    def OnCloseWindow(self, event):
        self.Destroy()
    

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = UrlInfoFrame()
    frame.Show()
    app.MainLoop()
